<?php
/***
 * 描述：节点管理模块
* @category
* @package		Lib
* @subpackage	Action.System
* @author		wanght <whtaok@gmail.com>
* @since		2013-4-6
* @version		$Id: NodeAction.class.php 3 2013-10-14 02:49:41Z whtaok@gmail.com $
*/
use Office as Office;
class NodeAction extends SystemAction {
	/**
	 * 描述：初始化节点模块
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		//$this->_initialize();
		parent::_initialize();
		$this->usingModel="SysAdminNode";
	}
	/**
	 * 描述：显示节点查询页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-6
	 */
	public function indexAct($group_name='',$module_name='') {
		$this->assign("group_name",$group_name);
		$this->assign("module_name",$module_name);
		
		if($group_name && $module_name){
			//显示指定分组，模型下的操作名称
			$this->initGroupList();
			$this->initModuleList($group_name);
			$this->initActionList($group_name,$module_name);
			$this->display('index_action');
		}else if($group_name){
			//显示指定分组下的模型名称
			$this->initGroupList();
			$this->initModuleList($group_name);
			$this->display('index_module');
		}else{
			$this->initGroupList();
			$this->display('index');
		}
	}
	
	/**
	 * 编辑节点
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-6
	 */
	public function doEditAct($group_name='',$module_name='',$action_name='') {
		
		if($action_name && $module_name && $group_name){
			//插入操作数据 action_level=3
			$data = array(
				"title" => $_POST['node_title'],
				"content" => $_POST['node_content'],
				"group_name" => $group_name,
				"module_name" => $module_name,
				"action_name" => $action_name,
				"action_params" => '',
				"ordlist" => 0,
				"action_level"=>3,
			);
			$params = array('group_name'=>$group_name,'module_name'=>$module_name);
		}elseif (!$action_name && $module_name && $group_name){
			//插入模块数据 action_level=2
			$data = array(
				"title" => $_POST['node_title'],
				"content" => $_POST['node_content'],
				"group_name" => $group_name,
				"module_name" => $module_name,
				"action_name" => '',
				"action_params" => '',
				"ordlist" => 0,
				"action_level"=>2,
			);
			$params = array('group_name'=>$group_name);
		}elseif (!$action_name && !$module_name && $group_name){
			//插入分组数据 action_level=1
			$data = array(
				"title" => $_POST['node_title'],
				"content" => $_POST['node_content'],
				"group_name" => $group_name,
				"module_name" => '',
				"action_name" => '',
				"action_params" => '',
				"ordlist" => 0,
				"action_level"=>1,
			);
			$params = array();
		}else{
			$this->success('数据错误', U('index',$params));
		}
		$mod = D('SysAdminNode');
		
		if($this->_param('id')){
			$data['id'] = $this->_param('id');
			$flag = $mod->where("id=".$data['id'])->save($data);
			if($flag!==false){
				$this->success('保存成功', U('index',$params));
			}
		}else{
			$mod->create($data);
			$flag = $mod->add();
			if($flag!==false){
				$this->success('新增成功', U('index',$params));
			}
		}
		//$this->success('操作失败', U('index'));
		
	}
	
	/**
	 * 初始化分组列表
	 */
	private function initGroupList(){
		//显示分组名称
		$group_names = explode(",", C("APP_GROUP_LIST"));
		foreach ($group_names as $key=>$val){
			$group_list[$key]['name']=$val;
			$group_list[$key]['title']="";
		}
		//分组节点是否保存在数据库中
		$this->assign("group_list",$group_list);
	
		//获得操作数据库信息
		$mod = D('SysAdminNode');
		$group_list_dbtmp = $mod->where(array('action_level'=>'1'))->select();
		$group_list_db = array();
		foreach ($group_list_dbtmp as $key=>$val){
			$group_list_db[$val['group_name']]=$val;
		}
		unset($group_list_dbtmp);
		$this->assign("group_list_db",$group_list_db);
	}
	/**
	 * 初始化模块列表
	 * @param unknown_type $group_name
	 */
	private function initModuleList($group_name){
		$group_path = LIB_PATH."Action/".$group_name;
		$module_filenames = scandir($group_path);
		foreach ($module_filenames as $key=>$val){
			$action_name = substr($val,0,-16);
			if($action_name){
				$module_list[$key]['name']=$action_name;
			}
		}
		$this->assign("module_list",$module_list);
	
		//获得操作数据库信息
		$mod = D('SysAdminNode');
		$module_list_dbtmp = $mod->where(array('group_name'=>$group_name,'action_level'=>'2'))->select();
		$module_list_db = array();
		foreach ($module_list_dbtmp as $key=>$val){
			$module_list_db[$val['module_name']]=$val;
		}
		unset($module_list_dbtmp);
		$this->assign("module_list_db",$module_list_db);
	
	}
	/**
	 * 初始化操作信息
	 * @param unknown_type $group_name
	 * @param unknown_type $module_name
	 */
	private function initActionList($group_name,$module_name){
		//跨模块调用
		$module_obj = A($group_name.'/'.$module_name);
		$module_class = new  ReflectionClass($module_obj);
		$module_methods = $module_class->getMethods(ReflectionMethod::IS_PUBLIC);
		foreach ($module_methods as $key=>$val){
			if(substr($val->name, -3)==='Act'){
				$action_list[$key]['name']=substr($val->name,0,-3);
			}
		}
		$this->assign("action_list",$action_list);
	
		//获得操作数据库信息
		$mod = D('SysAdminNode');
		$action_list_dbtmp = $mod->where(array('group_name'=>$group_name,'module_name'=>$module_name))->select();
		$action_list_db = array();
		foreach ($action_list_dbtmp as $key=>$val){
			$action_list_db[$val['action_name']]=$val;
		}
		unset($action_list_dbtmp);
		$this->assign("action_list_db",$action_list_db);
	}

}

?>